// import { createRouter, createWebHashHistory } from 'vue-router'
// import storage from '@/storage'
// import { message } from 'ant-design-vue'

// const router = createRouter({
//   history: createWebHashHistory(import.meta.env.BASE_URL),
//   routes: [
//     {
//       path: '/',
//       redirect: '/main',
//     },
//     {
//       path: '/main',
//       component: () => import('@/views/main/Main.vue'),
//       meta: {
//         keepAlive: true,
//       },
//     },
//     {
//       path: '/detail/:id',
//       component: () => import('@/views/detail/Detail.vue'),
//       meta: {
//         keepAlive: false,
//       },
//     },
//     {
//       path: '/idle',
//       component: () => import('@/views/idle/Idle.vue'),
//       meta: {
//         keepAlive: false,
//       },
//     },
//     {
//       path: '/manage',
//       component: () => import('@/views/manage/Manage.vue'),
//       meta: {
//         keepAlive: false,
//       },
//     },
//     {
//       path: '/user',
//       component: () => import('@/views/user-info/UserInfo.vue'),
//       meta: {
//         keepAlive: false,
//       },
//       children: [
//         {
//           path: 'shop',
//           component: () => import('@/views/user-info/layout/shop/shop.vue'),
//           meta: {
//             keepAlive: false,
//           },
//         },
//         {
//           path: 'publish',
//           component: () => import('@/views/user-info/layout/publish/publish.vue'),
//           meta: {
//             keepAlive: false,
//           },
//         },
//         {
//           path: 'info',
//           component: () => import('@/views/user-info/layout/info/info.vue'),
//           meta: {
//             keepAlive: false,
//           },
//         },
//       ],
//     },
//     {
//       path: '/:pathMatch(.*)',
//       name: 'NotFound',
//       component: () => import('@/views/not-found/NotFound.vue'),
//       meta: {
//         keepAlive: false,
//       },
//     },
//   ],
// })
// // 全局前置守卫
// router.beforeEach((to) => {
//   console.log('路由守卫', to.path)

//   if (to.path == '/main') {
//   } else {
//     if (!storage.get('token')) {
//       message.error('请先登录,再进行操作')
//       return '/main'
//     }
//   }
// })

// export default router

import { createRouter, createWebHashHistory } from 'vue-router'
import storage from '@/storage'
import { message } from 'ant-design-vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/main',
    },
    {
      path: '/main',
      component: () => import('@/views/main/Main.vue'),
      meta: {
        keepAlive: true,
        requiresAuth: false, // 明确不需要认证
      },
    },
    {
      path: '/detail/:id',
      component: () => import('@/views/detail/Detail.vue'),
      meta: {
        keepAlive: false,
        requiresAuth: true, // 需要认证
      },
    },
    {
      path: '/idle',
      component: () => import('@/views/idle/Idle.vue'),
      meta: {
        keepAlive: false,
        requiresAuth: true, // 需要认证
      },
    },
    {
      path: '/manage',
      component: () => import('@/views/manage/Manage.vue'),
      meta: {
        keepAlive: false,
        requiresAuth: true, // 需要认证
      },
    },
    {
      path: '/user',
      component: () => import('@/views/user-info/UserInfo.vue'),
      meta: {
        keepAlive: false,
        requiresAuth: true, // 父路由需要认证
      },
      children: [
        {
          path: 'shop',
          component: () => import('@/views/user-info/layout/shop/shop.vue'),
          meta: {
            keepAlive: false,
            requiresAuth: true, // 子路由需要认证
          },
        },
        {
          path: 'publish',
          component: () => import('@/views/user-info/layout/publish/publish.vue'),
          meta: {
            keepAlive: false,
            requiresAuth: true, // 子路由需要认证
          },
        },
        {
          path: 'order',
          component: () => import('@/views/user-info/layout/order/order.vue'),
          meta: {
            keepAlive: false,
            requiresAuth: true,
          },
        },
        {
          path: '/order/detal/:id',
          component: () => import('@/views/user-info/layout/order/detail.vue'),
          meta: {
            keepAlive: false,
            requiresAuth: true,
          },
        },
        {
          path: 'info',
          component: () => import('@/views/user-info/layout/info/info.vue'),
          meta: {
            keepAlive: false,
            requiresAuth: true, // 子路由需要认证
          },
        },
      ],
    },
    {
      path: '/:pathMatch(.*)',
      name: 'NotFound',
      component: () => import('@/views/not-found/NotFound.vue'),
      meta: {
        keepAlive: false,
        requiresAuth: false, // 404页面不需要认证
      },
    },
  ],
})

// 全局前置守卫
router.beforeEach((to) => {
  console.log('路由守卫检查:', to.path, '需要认证:', to.meta?.requiresAuth)

  // 如果路由需要认证且没有token
  if (to.meta?.requiresAuth && !storage.get('token')) {
    message.error('请先登录,再进行操作')
    return {
      path: '/main',
      query: { redirect: to.fullPath },
    }
  }
})

export default router
